#ParallelPlatform Project - A web platform to distribute and share knowledge
#Copyright (C) 2011  Daniel Guerrero Martinez & Sergio Rodriguez Lumley
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.contrib.auth.models import User
from django.db import models
from ParallelPlatform.wiki.models import Page
from django.utils.translation import ugettext as _

#class Profile(models.Model):
#    user = models.ForeignKey(User, unique=True)
#    collaborator = models.BooleanField(default=True)
#    owner = models.ManyToManyField(Page ,symmetrical=True, related_name="owner")

User.add_to_class('visibleContributor', models.BooleanField(default=True))
User.add_to_class('owner', models.ManyToManyField(Page ,symmetrical=True, related_name="owner", blank=True))


from django.contrib.auth.admin import UserAdmin
#UserAdmin.list_display += ('visibleContributor','owner')
#UserAdmin.list_filter += ('visibleContributor','owner')
UserAdmin.filter_horizontal += ('visibleContributor','owner')
UserAdmin.fieldsets += ( (_('Options page'), {'fields': ('visibleContributor', 'owner') }), )

#
#class CustomUserAdmin(UserAdmin):
#    fieldsets = (
#        (None, {'fields': ('username', 'password')}),
#        (_('Personal info'), {'fields': ('email',)}),
#        (_('Permissions'), {'fields': ('is_staff', 'is_active','is_superuser', 'user_permissions')}),
#        (_('Important dates'), {'fields': ('last_login','date_joined')}),
#        (_('Groups'), {'fields': ('groups',)}),
#        (_('Option Page'), {'fields': ('visibleContributor', 'owner')}),
#        )
#
#from django.contrib import admin
#
#admin.site.unregister(User)
#admin.site.register(User, CustomUserAdmin)

from django.contrib.auth.models import Group, Permission
from django.db.models import signals
from django.contrib.contenttypes.models import ContentType
from wiki import models as Wiki

#signals.post_syncdb.connect(initialGroups, sender=auth_app)
def initialGroups(sender, **kwargs):
    content_type = ContentType.objects.create(app_label="permissions_wiki",model="perm")
    #content_type_resources = ContentType.objects.get(app_label='ParallelPlatform.resources', model='Resources')
    perm = []
    perm.append(Permission.objects.create(codename='can_edit_page',content_type=content_type,
                                          name='Can edit page'))
    perm.append(Permission.objects.create(codename='can_create_page',content_type=content_type,
                                          name='Can create page'))
    perm.append(Permission.objects.create(codename='can_add_resources',content_type=content_type,
                                          name='Can add resources'))
    
    for p in perm:
        p.save()
    
    Contributor = Group.objects.create(name="Contributor")
    Contributor.permissions = perm
    Contributor.save()
    
    perm.append(Permission.objects.create(codename='can_disable_page',content_type=content_type,
                                          name='Can disable page'))
    perm.append(Permission.objects.create(codename='can_enable_page',content_type=content_type,
                                          name='Can enable page'))
    perm.append(Permission.objects.create(codename='can_erase_page',content_type=content_type,
                                          name='Can erase page'))
    perm.append(Permission.objects.create(codename='can_admin_page',content_type=content_type,
                                          name='Can admin page'))

    
    for p in perm:
        p.save()
    
    Supervisor = Group.objects.create(name="Supervisor")
    Supervisor.permissions = perm
    Supervisor.save()


signals.post_syncdb.connect(initialGroups,sender=Wiki)


